<template>
  <div id="claimslist">
    <h1 id="h1">查看所有理赔信息</h1>
    
    <div>
      <el-table
        :data="
          claimsList.filter(
            (data) =>
              !search ||
              data.address.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 100%"
      >
        <el-table-column label="编号" prop="_id"> </el-table-column>
         <!-- <el-table-column label="姓名" prop="name"> </el-table-column> -->
        <el-table-column label="事故发生时间" prop="time">
         
        </el-table-column>
        <el-table-column label="事故发生地址" prop="address"> </el-table-column>
        <el-table-column label="申请理赔原因" prop="reason"> </el-table-column>
       
        <el-table-column label="理赔图集">
          <template slot-scope="scope">
            <el-button @click="lookClaimsImages(scope.row)"
              >查看理赔凭证</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="state" label="理赔状态">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.state == 1">
              理赔申请中
            </el-tag>
            <el-tag type="success" v-else-if="scope.row.state == 2">
              理赔成功
            </el-tag>
            <el-tag type="danger" v-else-if="scope.row.state == 3">
              理赔失败
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column align="right">
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"
            />
          </template>
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 修改弹出框 -->
      <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="40%"
        center
      >
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="编号">
            <el-input v-model="form._id" disabled="disabled"></el-input>
          </el-form-item>
          <el-form-item label="事故发生地址">
            <el-input v-model="form.address"></el-input>
          </el-form-item>
          <el-form-item label="申请原因">
            <el-input type="textarea" v-model="form.reason"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页 -->
      <div class="block" id="fenye">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[4, 5, 6]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 查看理赔图集 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <!-- 渲染理赔图集 -->
        <div class="demo-image__lazy">
          <el-image v-for="(url,index) in urls" :key="index" :src=" 'http://localhost:3000/images/claimsimgs/' +url.imgUrl" lazy></el-image>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions, mapState, mapMutations } = createNamespacedHelpers(
  "claims"
);
export default {
  created() {
    this.getClaims();
  },
  data() {
    return {
      search: "", //搜索
      centerDialogVisible: false, //修改团出口
      dialogVisible: false, //理赔图集弹框
      //修改表单
      form: {
        time: "",
        _id: "",
        reason: "",
        address: "",
      },
      currentPage4: 1,
      urls: [],
    };
  },
  computed: {
    ...mapState(["claimsList", "total","cueerntPage"]),
  },
  methods: {
    ...mapActions(["getClaims", "modifyClaims"]),
    ...mapMutations(["getPageSize", "getCueerntPage"]),
    //修改理赔信息，状态为1 可以修改，状态为2 ，3 不可修改
    handleEdit(index, row) {
      this.centerDialogVisible = true;
      this.form = { ...row };
    },
    handleDelete(index, row) {
      this.$message({
        message: "不好意思！你没有权限！请开通权限",
        type: "warning",
      });
    },
    //修改提交
    async onSubmit() {
      this.centerDialogVisible = false;
      if (this.form.state == "1") {
        this.$message({
          message: "正在申请理赔中！不能修改信息！请等待",
          type: "warning",
        });
      } else {
        //修改理赔
        const data = await this.modifyClaims(this.form);
        if (data.data.status == "1") {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.getClaims();
        } else {
          this.$message.error("修改失败");
        }
      }
    },
    //每页几条数据
    handleSizeChange(val) {
      this.getPageSize(val);
      this.getClaims();
    },
    //当前页数
    handleCurrentChange(val) {
      this.getCueerntPage(val);
      this.getClaims();
    },
    //查看理赔图集
    lookClaimsImages(val) {
      this.dialogVisible = true;
      // console.log(758, val);
       this.urls=val.voucher
      console.log(this.urls);

    },
    //关闭理赔图集弹框
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
 watch:{
   cueerntPage(val){
       this.getClaims({cueerntPage:val});
   }
 }
};
</script>

<style scopde lang='scss'>
#claimslist {
  margin: 0;
  padding: 0;
  #h1 {
    border-bottom: 1px solid white;
    margin: 0;
    padding: 0;
    line-height: 50px;
    text-align: left;
  }
  #fenye {
    text-align: center;
    padding: 30px 0;
  }
  table{
    text-align: center
    ;
  }
}
</style>